package com.qfjg.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qfjg.entity.DumpRecord;
import com.qfjg.entity.DumpRecordQueryInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface DumpRecordMapper extends BaseMapper<DumpRecord> {



    @Select("SELECT * FROM dump_record WHERE project_id = #{projectId} ORDER BY TO_DATE(dump_date, 'YYYY/MM/DD') DESC, TO_TIMESTAMP(dump_time, 'HH24:MI') DESC")
    List<DumpRecord> getByProjectId(String projectId);



    @SelectProvider(type = SqlBuilder.class, method = "buildSelectDumpRecordListByQuery")
    List<DumpRecord> selectDumpRecordListByQuery(@Param("query") DumpRecordQueryInfo query);



    class SqlBuilder {
        public static String buildSelectDumpRecordListByQuery(@Param("query") DumpRecordQueryInfo query) {
            return new SQL(){{
                SELECT("*");
                FROM("dump_record");
                if (query.getProjectId() != null && !query.getProjectId().isEmpty()) {
                    WHERE("project_id = #{query.projectId}");
                }
                if (query.getPhoneNumber() != null && !query.getPhoneNumber().isEmpty()) {
                    WHERE("phone_number LIKE CONCAT('%', #{query.phoneNumber}, '%')");
                }
                if (query.getShipName() != null && !query.getShipName().isEmpty()) {
                    WHERE("ship_name = #{query.shipName}");
                }
                if (query.getShipBehavior() != null && !query.getShipBehavior().isEmpty()) {
                    WHERE("ship_behavior = #{query.shipBehavior}");
                }
                if (query.getDumpStartDate() != null && !query.getDumpStartDate().isEmpty()) {
                    WHERE("TO_DATE(dump_date, 'YYYY/MM/DD') >= TO_DATE(#{query.dumpStartDate}, 'YYYY/MM/DD')");
                }
                if (query.getDumpEndDate() != null && !query.getDumpEndDate().isEmpty()) {
                    WHERE("TO_DATE(dump_date, 'YYYY/MM/DD') <= TO_DATE(#{query.dumpEndDate}, 'YYYY/MM/DD')");
                }
                ORDER_BY("TO_DATE(dump_date, 'YYYY/MM/DD') DESC, TO_TIMESTAMP(dump_time, 'HH24:MI') DESC");
            }}.toString();
        }
    }
}
